package com.fancy.learncenter.ui.adapter;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.fancy.learncenter.R;
import com.fancy.learncenter.bean.CartoonWorkItemBean;
import com.fancy.learncenter.common.JumpIntentKey;
import com.fancy.learncenter.common.LoginUserSharePrefernce;
import com.fancy.learncenter.ui.activity.CartoonQuestionTypeActivity;
import com.fancy.learncenter.ui.activity.CartoonStaticsActivity;
import com.fancy.learncenter.ui.activity.CartoonWorkQuestionTypeActivity;
import com.fancy.learncenter.ui.adapter.base.CommonRecycleViewAdapter;
import com.fancy.learncenter.ui.adapter.base.CustomViewHold;

import java.util.List;

/**
 * Created by shiweiwei on 2017/12/7.
 */

public class FinishedWorkAdapter extends CommonRecycleViewAdapter<CartoonWorkItemBean> {
    public FinishedWorkAdapter(Context mContext, List<CartoonWorkItemBean> itemDatas) {
        super(mContext, R.layout.finished_work_item, itemDatas);
    }

    @Override
    public void bindView(CustomViewHold customViewHold, final CartoonWorkItemBean item, final int position) {

        TextView teach_book_name = customViewHold.getView(R.id.teach_book_name);
        TextView unit_name = customViewHold.getView(R.id.unit_name);
        TextView workReportText = customViewHold.getView(R.id.work_report);
        TextView turnip_num = customViewHold.getView(R.id.turnip_num);
        ImageView once_more_image = customViewHold.getView(R.id.once_more_image);

        int radishCount = Double.valueOf(item.getRadishCount()).intValue();
        if (!TextUtils.isEmpty(item.getRadishCount())) {
            turnip_num.setText("X" + radishCount);
        }
        if (!TextUtils.isEmpty(item.getUnitName())) {
            unit_name.setText(item.getUnitName());
        }
        if (!TextUtils.isEmpty(item.getLessonName())) {
            teach_book_name.setText(item.getLessonName());
        }

        workReportText.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                jumpToQuestion(JumpIntentKey.QUESTION_ENTER_TYPE.WORK_REPORT, item);

            }
        });

        once_more_image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setRecordTime(item);
                Bundle bundle = new Bundle();
                bundle.putSerializable(JumpIntentKey.QUESTION_ENTER, JumpIntentKey.QUESTION_ENTER_TYPE.DO_IT_AGAIN);
                bundle.putString(JumpIntentKey.STUDENTHOMEWORKID, String.valueOf(item.getStudentHomeworkId()));
                bundle.putString(JumpIntentKey.HOMEWORK_PACKAGE_ID, String.valueOf(item.getHomePackageId()));
                Intent intent = new Intent(mContext, CartoonWorkQuestionTypeActivity.class);
                intent.putExtras(bundle);
                mContext.startActivity(intent);
            }
        });

    }

    /**
     * 设置起始时间
     */
    private void setRecordTime(CartoonWorkItemBean item) {
        long time = System.currentTimeMillis();
        Log.i("startTime----", time + "");
        LoginUserSharePrefernce.setRecordStartTime(time, String.valueOf(item.getHomePackageId()));
    }


    private void jumpToQuestion(JumpIntentKey.QUESTION_ENTER_TYPE type, CartoonWorkItemBean item) {
        Intent intent = new Intent(mContext, CartoonStaticsActivity.class);
        intent.putExtra(JumpIntentKey.QUESTION_ENTER, type);
        intent.putExtra(JumpIntentKey.UNIT_NAME, item.getUnitName());
        intent.putExtra("releaseTime", item.getReleaseTime());
        intent.putExtra(JumpIntentKey.LESSON_NAME, item.getLessonName());
        intent.putExtra(JumpIntentKey.STUDENTHOMEWORKID, String.valueOf(item.getStudentHomeworkId()));
        intent.putExtra(JumpIntentKey.HOMEWORK_PACKAGE_ID, String.valueOf(item.getHomePackageId()));
        mContext.startActivity(intent);
    }
}
